package com.health.mapp.impl;

import com.health.commons.MD5Password;
import com.health.db.model.UserLIST;
import com.health.db.model.UserPreRegisterInfo;
import com.health.db.service.user.IUserLISTService;
import com.health.db.service.user.IUserPreRegisterInfoService;
import com.health.mapp.bean.AddPreRegisterInfoReq;
import com.health.mapp.bean.AddPreRegisterInfoResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import org.apache.http.util.TextUtils;

public class AddPreRegisterInfoImpl implements ActionInterface{

    private IUserLISTService userLISTService;
    private IUserPreRegisterInfoService userPreRegisterInfoService;

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {
        AddPreRegisterInfoReq req= (AddPreRegisterInfoReq) reqBean;
        AddPreRegisterInfoResp _resp=new AddPreRegisterInfoResp();
        if (TextUtils.isEmpty(req.getUserNo()))
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("手机号不能为空");
        } else if (TextUtils.isEmpty(req.getPassword()))
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("密码不能为空");
        }else if (TextUtils.isEmpty(req.getUserName()))
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("昵称不能为空");
        }else
        {
            UserLIST userLIST=userLISTService.selectByUserNo(req.getUserNo());
            if (null!=userLIST)
            {
                _resp.setErrorCode(-1);
                _resp.setErrorMsg("该手机号已经被注册过");
            }else
            {
                UserPreRegisterInfo preRegisterInfo=new UserPreRegisterInfo();
                preRegisterInfo.setUserNo(req.getUserNo());
                preRegisterInfo.setUserName(req.getUserName());
                preRegisterInfo.setPassword(MD5Password.getEncodedPassword_Hex(req.getPassword()));
                int result=userPreRegisterInfoService.addUserPreRegisterInfo(preRegisterInfo);

                if (result>0)
                {
                    _resp.setErrorCode(0);
                    _resp.setErrorMsg("success");
                    _resp.setUserNo(preRegisterInfo.getUserNo());
                }else
                {
                    _resp.setErrorCode(-1);
                    _resp.setErrorMsg("注册失败");
                }
            }
        }
        return _resp;
    }

    public IUserLISTService getUserLISTService() {
        return userLISTService;
    }

    public void setUserLISTService(IUserLISTService userLISTService) {
        this.userLISTService = userLISTService;
    }

    public IUserPreRegisterInfoService getUserPreRegisterInfoService() {
        return userPreRegisterInfoService;
    }

    public void setUserPreRegisterInfoService(IUserPreRegisterInfoService userPreRegisterInfoService) {
        this.userPreRegisterInfoService = userPreRegisterInfoService;
    }
}
